
/*! 
 * \file       sx1276-Hal.c
 * \brief      SX1276 Hardware Abstraction Layer
 *
 * \version    2.0.B2 
 * \date       Nov 21 2012
 * \author     Miguel Luis
 *
 * Last modified by Miguel Luis on Jun 19 2013
 */
#include <stdint.h>
#include <stdbool.h> 
#include <stdio.h>
#include "stm32f10x.h"
#include "../bsp_spi/bsp_spi.h"
#include "sx1276-Hal.h"








void SX1276InitIo( void )
{
    GPIO_InitTypeDef GPIO_InitStructure;

    /*这里是配置LORA的频段选择管脚*/	
    RCC_APB2PeriphClockCmd(LORA_ADDR_BIT0_CLK,ENABLE);
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Pin = LORA_ADDR_BIT0_PIN;
    GPIO_Init( LORA_ADDR_BIT0_PORT, &GPIO_InitStructure );
	
    RCC_APB2PeriphClockCmd( LORA_ADDR_BIT1_CLK,ENABLE);
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Pin = LORA_ADDR_BIT1_PIN;
    GPIO_Init( LORA_ADDR_BIT1_PORT, &GPIO_InitStructure );
	
    RCC_APB2PeriphClockCmd( LORA_ADDR_BIT2_CLK,ENABLE);
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Pin = LORA_ADDR_BIT2_PIN;
    GPIO_Init( LORA_ADDR_BIT2_PORT, &GPIO_InitStructure );
	
    RCC_APB2PeriphClockCmd( LORA_ADDR_BIT3_CLK,ENABLE);
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Pin = LORA_ADDR_BIT3_PIN;
    GPIO_Init( LORA_ADDR_BIT3_PORT, &GPIO_InitStructure );
	
    /*这里是配置LORA的片选管脚*/
	RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA,ENABLE);
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Pin = NSS_PIN;
    GPIO_Init( NSS_IOPORT, &GPIO_InitStructure );
}

void SX1276SetReset( uint8_t state )
{
    GPIO_InitTypeDef GPIO_InitStructure;

	  RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA,ENABLE);
		GPIO_InitStructure.GPIO_Pin = RESET_PIN;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
    GPIO_Init( RESET_IOPORT, &GPIO_InitStructure );
	
    if( state == RADIO_RESET_ON )
    {
        GPIO_WriteBit( RESET_IOPORT, RESET_PIN, Bit_RESET );
    }
    else
    {
        GPIO_WriteBit( RESET_IOPORT, RESET_PIN, Bit_SET );
    }
}

void SX1276Write( uint8_t addr, uint8_t data )
{
    SX1276WriteBuffer( addr, &data, 1 );
}

void SX1276Read( uint8_t addr, uint8_t *data )
{
    SX1276ReadBuffer( addr, data, 1 );
}

void SX1276WriteBuffer( uint8_t addr, uint8_t *buffer, uint8_t size )
{
    uint8_t i;

    //NSS = 0;
    GPIO_WriteBit( NSS_IOPORT, NSS_PIN, Bit_RESET );

    SpiInOut( addr | 0x80 );
    for( i = 0; i < size; i++ )
    {
        SpiInOut( buffer[i] );
    }

    //NSS = 1;
    GPIO_WriteBit( NSS_IOPORT, NSS_PIN, Bit_SET );
}

void SX1276ReadBuffer( uint8_t addr, uint8_t *buffer, uint8_t size )
{
    uint8_t i;

    //NSS = 0;
    GPIO_WriteBit( NSS_IOPORT, NSS_PIN, Bit_RESET );

    SpiInOut( addr & 0x7F );

    for( i = 0; i < size; i++ )
    {
        buffer[i] = SpiInOut( 0 );
    }

    //NSS = 1;
    GPIO_WriteBit( NSS_IOPORT, NSS_PIN, Bit_SET );
}

void SX1276WriteFifo( uint8_t *buffer, uint8_t size )
{
    SX1276WriteBuffer( 0, buffer, size );
}

void SX1276ReadFifo( uint8_t *buffer, uint8_t size )
{
    SX1276ReadBuffer( 0, buffer, size );
}



